package com.mycompany.unity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mycompany.entity.Number;


public class NumberUnity {
	
	
	
public static Map<String,Map<String,Integer>> sortingNumber(List<Number> listNumber){
		
		Map<String,Map<String,Integer>> result = new HashMap<String,Map<String,Integer>>();
		List<Number> listTop = new ArrayList<Number>();
		List<Number> listDown = new ArrayList<Number>();
		List<Number> listTod = new ArrayList<Number>();
		List<Number> listTang = new ArrayList<Number>();
		List<Number> listTangdown = new ArrayList<Number>();
		List<Number> listUnknow = new ArrayList<Number>();
		
		for(Number num : listNumber){
			
			if("top".equals(num.getType())){
				listTop.add(num);
			}else
			if("down".equals(num.getType())){
				listDown.add(num);
			}else
			if("tod".equals(num.getType())){
				listTod.add(num);
			}else
			if("tang".equals(num.getType())){
				listTang.add(num);
			}else
			if("tangdown".equals(num.getType())){
				listTangdown.add(num);
			}
			else{
				listUnknow.add(num);
			}
		}
		
		
		// Summary Price per number
		Map<String,Integer> summaryTop = new HashMap<String,Integer>();
		Map<String,Integer> summaryDown = new HashMap<String,Integer>();
		Map<String,Integer> summaryTod = new HashMap<String,Integer>();
		Map<String,Integer> summaryTang = new HashMap<String,Integer>();
		Map<String,Integer> summaryTangdown = new HashMap<String,Integer>();
		Map<String,Integer> summaryUnknow = new HashMap<String,Integer>();
		Integer sum = 0;
		// top 
		for(Number num: listTop){
			
			if(summaryTop.get(num.getNumber()) == null){
				summaryTop.put(num.getNumber(), num.getPrice());
				
			}else{
				sum = summaryTop.get(num.getNumber());
				summaryTop.remove(num.getNumber());
				summaryTop.put(num.getNumber(), num.getPrice()+sum);
				sum=0;
			}
			
		}
		
		// down
		for(Number num: listDown){
			if(summaryDown.get(num.getNumber()) == null){
				summaryDown.put(num.getNumber(), num.getPrice());
				
			}else{
				sum = summaryDown.get(num.getNumber());
				summaryDown.remove(num.getNumber());
				summaryDown.put(num.getNumber(), num.getPrice()+sum);
				sum=0;
			}

		}
		
		// tod
		for(Number num: listTod){
			if(summaryTod.get(num.getNumber()) == null){
				summaryTod.put(num.getNumber(), num.getPrice());
				
			}else{
				sum = summaryTod.get(num.getNumber());
				summaryTod.remove(num.getNumber());
				summaryTod.put(num.getNumber(), num.getPrice()+sum);
				sum=0;
			}
		}
		
		// tang
		for(Number num: listTang){
			if(summaryTang.get(num.getNumber()) == null){
				summaryTang.put(num.getNumber(), num.getPrice());
				
			}else{
				sum = summaryTang.get(num.getNumber());
				summaryTang.remove(num.getNumber());
				summaryTang.put(num.getNumber(), num.getPrice()+sum);
				sum=0;
			}
		}
		
		// tangdown
		for(Number num: listTangdown){
			if(summaryTangdown.get(num.getNumber()) == null){
				summaryTangdown.put(num.getNumber(), num.getPrice());
				
			}else{
				sum = summaryTangdown.get(num.getNumber());
				summaryTangdown.remove(num.getNumber());
				summaryTangdown.put(num.getNumber(), num.getPrice()+sum);
				sum=0;
			}
		}
				
		// unknow
		for(Number num: listUnknow){
			if(summaryUnknow.get(num.getNumber()) == null){
				summaryUnknow.put(num.getNumber(), num.getPrice());
				
			}else{
				sum = summaryUnknow.get(num.getNumber());
				summaryUnknow.remove(num.getNumber());
				summaryUnknow.put(num.getNumber(), num.getPrice()+sum);
				sum=0;
			}
		}
		
		// top list
		result.put("top",summaryTop);
		// down list
		result.put("down",summaryDown);
		// tod list
		result.put("tod",summaryTod);
		// tang list
		result.put("tang",summaryTang);
		// tangdown list
		result.put("tangdown",summaryTangdown);
		// unknow list
		result.put("unknow",summaryUnknow);
		return result;
	}
	
	public static void printMap(Map<String,Map<String,Integer>> mapNumber){
		System.out.println("************************************************************");
		for (String name: mapNumber.keySet()){

            String key =name.toString();
            Map<String,Integer> map = mapNumber.get(key);  
           
            System.out.println("******************************"+key+"******************************");
            System.out.println(map);
            System.out.println("************************************************************");
		} 

	}
	
	
	public static Map<String,Integer> numberSummaryTotal(List<Number> numbers){
		Map<String,Integer> totals = new HashMap<String, Integer>();
		Integer all = 0;
		Integer top = 0;
		Integer down = 0;
		Integer tang = 0;
		Integer tangdown = 0;
		Integer tod = 0;
		for(Number num : numbers){
			
				all=all+num.getPrice();
				
				if("top".equals(num.getType())){
					top = top+num.getPrice();
				}
				if("down".equals(num.getType())){
					down = down+num.getPrice();
				}
				if("tang".equals(num.getType())){
					tang = tang+num.getPrice();
				}
				if("tangdown".equals(num.getType())){
					tangdown = tangdown+num.getPrice();
				}
				if("tod".equals(num.getType())){
					tod = tod+num.getPrice();
				}
		}
		totals.put("all", all);
		totals.put("top", top);
		totals.put("down", down);
		totals.put("tang", tang);
		totals.put("tangdown", tangdown);
		totals.put("tod", tod);
		
		return totals;
	}
	
	public static Map<String,Integer> setPercen(){
		Map<String,Integer> result = new HashMap<String, Integer>();
		
		result.put(Constants.NAME_PERCEN_GET, Constants.PERCEN_GET);
		result.put(Constants.NAME_PERCEN_GIVE, Constants.PERCEN_GIVE);
		result.put(Constants.NAME_PERCEN_GIVE_CUSTOMER, Constants.PERCEN_GIVE_CUSTOMER);
		result.put(Constants.NAME_PERCEN_GIVE_PARTNER, Constants.PERCEN_GIVE_PARTNER);
		
		result.put(Constants.NAME_LUCKY_TOP, Constants.LUCKY_TOP);
		result.put(Constants.NAME_LUCKY_DOWN, Constants.LUCKY_DOWN);
		result.put(Constants.NAME_LUCKY_TANG, Constants.LUCKY_TANG);
		result.put(Constants.NAME_LUCKY_TOD, Constants.LUCKY_TOD);
		result.put(Constants.NAME_LUCKY_TANGDOWN, Constants.LUCKY_TANGDOWN);
		
		
		result.put(Constants.NAME_LUCKY_GIVE_TOP, Constants.LUCKY_GIVE_TOP);
		result.put(Constants.NAME_LUCKY_GIVE_DOWN, Constants.LUCKY_GIVE_DOWN);
		result.put(Constants.NAME_LUCKY_GIVE_TANG, Constants.LUCKY_GIVE_TANG);
		result.put(Constants.NAME_LUCKY_GIVE_TANGDOWN, Constants.LUCKY_GIVE_TANGDOWN);
		result.put(Constants.NAME_LUCKY_GIVE_TOD, Constants.LUCKY_GIVE_TOD);
		
		return result;
	}
	
}
